<?php

class morders extends CI_Model{
    
    private $tbl = "orders";
    public $_last_id;
    
    public function fetchAll($numb = NULL, $off = NULL) {
        if ($numb == Null && $off == NULL)
            return $this->db->order_by("id", "desc")->get($this->tbl)->result();
        else
            return $this->db->order_by("id", "desc")->get($this->tbl, $numb, $off)->result();
    }
    
    public function fetchOne($id){
        return $this->db->where('id',$id)->get($this->tbl)->row();
    }
    
    public function insert($arr = array()){
        $this->db->insert($this->tbl,$arr);
        $this->_last_id = $this->db->insert_id();
    }
    
    public function update($id,$arr = array()){
        $this->db->where('id',$id)->update($this->tbl,$arr);
    }
    
    public function delete($id){
        $this->db->delete($this->tbl, array('id' => $id)); 
    }
    
    public function deleteAll(){
        $this->db->empty_table($this->tbl); 
    }
    
    public function getByCusId($id){
        return $this->db->where('cus_id',$id)->order_by("id", "desc")->get($this->tbl)->result();
    }
    
    public function getStatByDay(){
        return $this->db->select('DATE(`order_time`) as order_time,DAY(`order_time`) as time, status')->select_sum('total')->group_by('DATE(`order_time`)')->having('status', 1)->order_by("order_time", "desc")->get($this->tbl)->result();
    }
    
    public function getStatByMonth(){
        return $this->db->select('MONTH(`order_time`) as order_time1, YEAR(`order_time`) as order_time2, status')->select_sum('total')->group_by('MONTH(`order_time`)')->having('status', 1)->order_by('DATE(`order_time`)', "desc")->get($this->tbl)->result();
    }
}
